using System;
using System.Collections.Generic;
using System.Text;
using SdlDotNet.Input;

namespace SnakeCharp
{
    /// <summary>
    /// This class is responsible for holding all inputs from the player.
    /// </summary>
    public class Controller
    {
        private List<Key> keys;

        /// <summary>
        /// Initializes a new instance of the <see cref="Controller"/> class.
        /// </summary>
        public Controller()
        {
            this.keys = new List<Key>();
        } 

        #region Public Methods

        /// <summary>
        /// Check if a certain key is being pressed
        /// </summary>
        /// <param name="check">Key to check</param>
        /// <returns>Returns if a certain key is pressed or not</returns>
        public bool CheckKey(Key key)
        {
            foreach (Key temp in this.keys)
            {
                if (temp == (key))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Hook this to the SDL event handler to update the key down input data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">Keybord event argument</param>
        public void KeyDown(object sender, KeyboardEventArgs e)
        {            
            TryToAdd(e.Key);
        }

        /// <summary>
        /// Hook this to the SDL event handler to update the key up input data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">Keybord event argument</param>
        public void KeyUp(object sender, KeyboardEventArgs e)
        {
            DeleteKey(e.Key);
        }

        /// <summary>
        /// Clears the key collection.
        /// </summary>
        public void ClearKey()
        {
            this.keys.Clear();
        }

        /// <summary>
        /// Adds the key to the collection.
        /// </summary>
        /// <param name="add">The add.</param>
        public void AddKey(Key key)
        {
            TryToAdd(key);
        }

        public override string ToString()
        {
            StringBuilder temp = new StringBuilder();
            foreach (Key tempString in this.keys)
            {
                temp.Append(tempString.ToString());
            }
            return temp.ToString();
        }

        #endregion


        #region Private Methods

        /// <summary>
        /// Deletes the key.
        /// </summary>
        /// <param name="key">The key.</param>
        private void DeleteKey(Key key)
        {
            foreach (Key temp in this.keys)
            {
                if (temp == key)
                {
                    this.keys.Remove(key);
                    return;
                }
            }
        }

        /// <summary>
        /// Tries to add the key to the collection.
        /// If it's already there is will not be added.
        /// </summary>
        /// <param name="key">The key.</param>
        private void TryToAdd(Key key)
        {
            foreach (Key temp in this.keys)
            {
                if (temp == key)
                {
                    return;
                }
            }
            this.keys.Add(key);
        } 

        #endregion


    }
}
